package com.netdimensions.confluence.plugins;

import com.atlassian.confluence.pages.PageManager;
import com.atlassian.confluence.spaces.SpaceManager;
import com.atlassian.confluence.user.AuthenticatedUserThreadLocal;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import java.util.Arrays;
import java.util.Map;

public final class UserNewsMacro extends EkpMacro {
    public UserNewsMacro() {
    }

    protected final String html(final Element root) {
        final StringBuilder result = new StringBuilder();
        result.append("<div class=\"rssMacro\">");
        result.append("<table>");

        for (final Node entry : XPathUtils.nodes("entry", root)) {
            result.append("<tr>");
            result.append("<td valign=\"top\">");
            result.append("<b><a href=\"");
            result.append(XPathUtils.string("link[@rel=\"alternate\"]/@href", entry));
            result.append("\">");
            result.append(XPathUtils.string("title", entry));
            result.append("</a></b>");
            result.append("<br />");
            result.append("<span class=\"smalltext\">");
            result.append(XPathUtils.string("summary", entry));
            result.append("</span>");
            result.append("</td>");
            result.append("</tr>");
        }

        result.append("</table>");
        result.append("</div>");
        return result.toString();
    }

    protected final String url(final Map params) {
        return UrlUtils.url("userNews", Arrays.asList(new Parameter("userId", AuthenticatedUserThreadLocal.getUser().getName()), new Parameter("format", "atom")));
    }
}